package com.GUI.lesson03;

import java.awt.*;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.Iterator;

//鼠标监听事件
public class TestMouseListener {
    public static void main(String[] args) {
        new MyFrame("画图");
    }
}
//自己的类
class MyFrame extends Frame{
    //声明集合
    ArrayList points;
     //画画需要画笔，需要监听鼠标位置，需要集合来储存这个点
    public MyFrame(String name){
        //基础属性
        super(name);
        setVisible(true);
        setBounds(200,200,800,800);

        //存储鼠标点击生成的x，y。
        points = new ArrayList<>();

        //鼠标监听器，针对这个窗口
        this.addMouseListener(new MyMouseListener());
    }

    @Override
    public void paint(Graphics g) {
        //画画，监听鼠标的事件
        Iterator iterator = points.iterator();
        while (iterator.hasNext()){
            Point point =(Point) iterator.next();
            g.setColor(Color.blue);
            g.fillOval(point.x,point.y,10,10);
        }
    }
    public void addPaint(Point point){
        points.add(point);
    }
    //适配器模式
    private class MyMouseListener extends MouseAdapter {
        //鼠标 按下，弹起，按住不放。
        @Override
        public void mousePressed(MouseEvent e) {
            MyFrame frame =(MyFrame) e.getSource();
            frame.addPaint(new Point(e.getX(),e.getY()));
            //每次新增点，需要刷新，重新画
            frame.repaint();//刷新
        }
    }

   /* private class MyMouseListener implements MouseListener {

        @Override
        public void mouseClicked(MouseEvent e) {

        }

        @Override
        public void mousePressed(MouseEvent e) {

        }

        @Override
        public void mouseReleased(MouseEvent e) {

        }

        @Override
        public void mouseEntered(MouseEvent e) {

        }

        @Override
        public void mouseExited(MouseEvent e) {

        }
    }*/
}